﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.service;
using DXApplicationTangche.DTO;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.Utils.Win;
using DevExpress.XtraLayout;

namespace DXApplicationTangche.UC.订单一览新.辅料.辅料管理
{
    public partial class Form辅料BOM维护 : DevExpress.XtraEditors.XtraForm
    {
        private DTO辅料一览 dTO辅料一览 = new DTO辅料一览();
        private String subMaterialTypeId;
        private String subMaterialType;
        private Boolean is更新 = false;

        public Form辅料BOM维护()
        {
            InitializeComponent();
            this.searchLookUpEdit辅料类别.Properties.DataSource = 辅料Service.get辅料类别一览(false);
        }

        #region 选择辅料类别
        private void searchLookUpEdit辅料类别_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit辅料类别View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit辅料类别.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.subMaterialTypeId = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogId").ToString();
                    this.subMaterialType = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogName").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅料类别.ToolTip = null;
            searchLookUpEdit辅料类别.EditValue = null;
        }
        private void searchLookUpEdit辅料类别_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.subMaterialType;
        }
        #endregion


        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(this.textBox辅料编码.Text))
            {
                this.dTO辅料一览.subMaterialCode = this.textBox辅料编码.Text.Trim();
                this.dTO辅料一览.subMaterialPicture = this.textBox图片.Text.Trim();
            }
            if (String.IsNullOrWhiteSpace(this.subMaterialTypeId))
            {
                MessageBox.Show("请选择辅料类别");
                return;
            }
            this.dTO辅料一览.subMaterialTypeId = this.subMaterialTypeId;
            this.dTO辅料一览.subMaterialType = this.subMaterialType;
            this.dTO辅料一览.subMaterialSupplier = this.textBox供应商.Text.Trim();
            this.dTO辅料一览.subMaterialNumber = this.textBox品番.Text.Trim();
            this.dTO辅料一览.subMaterialName = this.textBox辅料名称.Text.Trim();
            this.dTO辅料一览.subMaterialSpecDesc = this.textBox规格.Text.Trim();
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
                ofd.Title = "请选择图片文件";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    //不用
                    String subMaterialCode = 辅料Service.post辅料保存(this.dTO辅料一览, ofd);
                    //if (!is更新)
                    //{
                    //    try
                    //    {
                    //        if (!String.IsNullOrEmpty(subMaterialCode))
                    //        {
                    //            //不用
                    //            DTO辅料一览 dto = 辅料Service.getAccessoryByAccessoryCode(subMaterialCode);
                    //            辅料Service.金蝶辅料保存("VEN00004", dto);
                    //        }
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        Console.WriteLine(ex.Message);
                    //        MessageBox.Show("金蝶未保存");
                    //    }
                    //}
                    MessageBox.Show("操作完成");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败");
            }
            this.Close();
        }
    }
}